﻿// ***********************************************************************
// Assembly         : STX.Lib
// Author           : seven.x
// Created          : 12-26-2013
//
// Last Modified By : seven.x
// Last Modified On : 12-26-2013
// ***********************************************************************
// <copyright file="SystemExtensions.cs" company="驰创数码">
//     Copyright (c) 驰创数码. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************using System.Collections.Generic;

using System.ComponentModel;
using System.Reflection;

namespace System
{
    /// <summary>
    /// Class SystemExtensions.
    /// </summary>
    public static class SystemExtensions
    {
        /// <summary>
        /// Lasts the day of month.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <returns>DateTime.</returns>
        public static DateTime LastDayOfMonth(this DateTime dt)
        {
            var startDate = new DateTime(dt.Year, dt.Month, 1);
            var endDate = startDate.AddMonths(1);
            return new DateTime(dt.Year, dt.Month, (endDate - startDate).Days);
        }

        /// <summary>
        /// Gets the current time mills.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <returns>System.Double.</returns>
        public static double GetCurrentTimeMills(this DateTime dt)
        {
            var ts = dt - DateTime.Parse("1970-01-01 00:00:00");
            return ts.TotalMilliseconds;
        }

        public static string GetFiledName(this Enum value)
        {
            var enumInfo = value.GetType().GetField(value.ToString());
            if (enumInfo == null) return "";
            var enumAttributes = (FieldAttribute[])enumInfo.
                GetCustomAttributes(typeof(FieldAttribute), false);
            if (enumAttributes.Length > 0)
            {
                return enumAttributes[0].Name;
            }
            return "";
        }
    }
}
